
# coding:utf-8

from lib import flux, al
from lib.fluxext import singleton
from system import phy
from utils.suger import *
from utils import dir2vec, dir2vecx


class Skill(object):
    def __init__(self, id, cd):
        self.id = id
        self.cd = cd
        self.stmt = {}
        self.cost = {}

    def get_level(self, use_time):
        return 1

    def check(self, unit, level):
        '''法术释放检查，返回0则无问题，返回2不满足施法条件，返回3不满足法术消耗'''
        # 是否满足施法条件
        for k,v in self.stmt.items():
            _v = unit.attr[k]
            if _v and _v < v:
                return 2

        # 是否满足施法消耗
        for k,v in self.cost.items():
            _v = unit.attr[k]
            if _v and _v < v:
                return 3

    def cast(self, unit, level):
        self.do_cast(unit, level)
        for k,v in self.cost.items():
            _v = unit.attr[k]
            if _v:
                unit.attr[k] -= v

    def do_cast(self, unit, level):
        pass

    def get_text(self):
        return ''

# 闪现 id=1
class ShanXian(Skill):
    def __init__(self):
        super(ShanXian, self).__init__(1, 2)
        self.sprite = 'data/pics/skill/1.png'
        self.name = '闪现'

        self.cost = {
            'mp' : 2,
        }

    def do_cast(self, unit, level):
        dist = 200

        pos = unit.GetPos()
        size = unit.GetSize()

        _dir = unit.GetDir()
        body = unit.body

        start = dir2vecx(_dir, size.x/2+1, size.y/2+1)
        end = dir2vecx(_dir, size.x+1 + dist, size.y+1 + dist)

        ret = phy.space.segment_query_first((pos.x+start[0], pos.y+start[1]), (pos.x+end[0], pos.y+end[1]))

        if not ret:
            offset = dir2vec(_dir, dist)
            body.position = pos.x + offset[0], pos.y + offset[1]
        else:
            dist = ret.get_hit_distance()
            offset = dir2vec(_dir, dist)
            body.position = pos.x + offset[0], pos.y + offset[1]

        #print phy.space.nearest_point_query((100,0), 100)
        #print pymunk.SegmentQueryInfo(body, (0,0), (100,0), 1, 1)

    def get_text(self):
        return '%s\n向正前方移动一段距离。' % self.name

# 恢复术 id=2
class HuiFu(Skill):
    def __init__(self):
        super(HuiFu, self).__init__(2, 5)
        self.sprite = 'data/pics/skill/2.png'
        self.cd = 5
        self.name = '恢复术'
        
        self.cost = {
            'mp' : 1,
        }

    def do_cast(self, unit, level):
        unit.attr['hp'] += 3

    def get_text(self):
        return '%s\n治疗自己。' % self.name


# 火球术 id=3
class HuoQiu(Skill):
    def __init__(self):
        super(HuoQiu, self).__init__(3, 7)
        self.sprite = 'data/pics/skill/3.png'
        self.name = '火球术'
        
        self.cost = {
            'mp' : 2,
        }

    def do_cast(self, unit, level):
        from views.bullet import Bullet
        bullet = Bullet(flux.ParticleView)
        bullet.v.AddCircle(3)
        bullet.v.SetMaxParticles(500)
        bullet.v.SetParticlesPerSecond(40.0)
        bullet.v.SetParticleLifetime(0.5)
        bullet.v.SetSpread(3.14)
        bullet.v.SetSpeedRange(180,200)
        bullet.v.SetEndScale(0.5)
        bullet.v.SetEndColor(flux.utils.Color(1.0, 1.0, 1.0, 0.0))
        bullet.v.SetGravity(flux.utils.Vec2f(100,0))
        #bullet.v.SetAttractor(flux.utils.Vec2f(50, 0))
        #bullet.v.SetAttractorStrength(600)

        pos = unit.GetMapPos()
        _dir = unit.GetDir()
        _x, _y = 0, 0
        if _dir == al.KEY_LEFT: _x = -10
        elif _dir == al.KEY_RIGHT: _x = 10
        elif _dir == al.KEY_UP: _y = -10
        elif _dir == al.KEY_DOWN: _y = 10
        
        #bullet.v.SetSize(50, 50)
        bullet.v.SetColor(1,0,0)
        bullet.SetMapMove(_x, _y, 0.7)
        bullet.v.SetSystemLifetime(0.7)
        bullet.v.OnMessage = None
        def A():
            pass
        bullet.v.OnDestroy = A
        bullet.SetMapPos(pos[0], pos[1])
        bullet.AddToScreen(Game().scr, 3)
        
    def get_text(self):
        return '%s\n释放一个火球，对落点周围敌人造成伤害。' % self.name



# 奥术强化 id=4
class AoShuQiangHua(Skill):
    def __init__(self):
        super(AoShuQiangHua, self).__init__(4, 20)
        self.sprite = 'data/pics/skill/4.png'
        self.name = '奥术强化'
        
        self.cost = {
            'mp' : 2,
        }

    def do_cast(self, unit, level):
        unit.attr['sp'] += 5

        '''self.powerup = flux.View()
        self.powerup.SetSize(10, 10)
        self.powerup.SetColor(0,0,1)
        self.powerup.SetAnchor(unit.anchor)
        self.powerup.AddToScreen(Game().scr, 1)'''

    def get_text(self):
        return '%s\n提升法术强度，持续一段时间。' % self.name


SKILL_LST = {
    1 : ShanXian(),
    2 : HuiFu(),
    3 : HuoQiu(),
    4 : AoShuQiangHua(),
}
